package components.contorls.superClass
{
    import components.core.UIComponent;
    import components.managers.PopUpManager;
    import components.utils.EventListenerUtil;
    
    import flash.display.DisplayObject;
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;

    public class BaseWindow extends UIComponent
    {
        private var _title:String = "window";

        public function BaseWindow()
        {
            super();

			if(panel)
			{
				EventListenerUtil.addEventListener( panel.closeButton,MouseEvent.CLICK, closeButton_clickHandler);
				EventListenerUtil.addEventListener( panel.moveMC,MouseEvent.MOUSE_DOWN, moveMC_mouseDownHandler);
	            _title = panel.title;
			}
			else if(closeBtn)
			{
				EventListenerUtil.addEventListener( closeBtn,MouseEvent.CLICK, closeButton_clickHandler);
			}
        }

        /***************************************
         *
         * 显示对象声明
         *
         * **************************************/

        public function get panel():BasePanel
        {
            return getChildByName("panelSkin") as BasePanel;
        }
		
		public function get closeBtn():BaseButton
		{
			return getChildByName("closeBtnSkin") as BaseButton;
		}

        /***************************************
         *
         * 验证
         *
         * **************************************/

        protected override function commitProperties():void
        {
            super.commitProperties();
        }

        protected override function measureSizes():Boolean
        {
            if (super.measureSizes())
            {
				if(panel)
				{
	                panel.width = width;
	                panel.height = height;
				}
				
                return true;
            }

            return false;
        }


        protected override function updateDisplayList():void
        {
            super.updateDisplayList();
        }

        /***************************************
         *
         * 组件定义
         *
         * **************************************/

        /***************************************
         *
         * 其他代码
         *
         * **************************************/

        public function get title():String
        {
            return _title;
        }

        public function set title(value:String):void
        {
            _title = value;
			
			if(panel)
            	panel.title = title;
        }

        protected function moveMC_mouseDownHandler(event:Event):void
        {
			EventListenerUtil.addEventListener(this,MouseEvent.MOUSE_UP, moveMC_mouseUpHandler);
            startDrag();
        }

        protected function moveMC_mouseUpHandler(event:MouseEvent):void
        {
            removeEventListener(MouseEvent.MOUSE_UP, moveMC_mouseUpHandler);
            stopDrag();
            invalidate();
        }

        protected function closeButton_clickHandler(event:MouseEvent):void
        {
            dispatchEvent(new Event(Event.CLOSE));
        }

        public override function destroy():void
        {
            super.destroy();

            removeEventListener(MouseEvent.MOUSE_UP, moveMC_mouseUpHandler);
			
			if(panel)
			{
	            panel.closeButton.removeEventListener(MouseEvent.CLICK, closeButton_clickHandler);
	            panel.moveMC.removeEventListener(MouseEvent.MOUSE_DOWN, moveMC_mouseDownHandler);
	            panel.destroy();
			}
        }

    }
}
